home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / network.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-29  |  8.3 KB  |  403 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** connection code
  36. */
  37.  
  38. #include "common.h"
  39. #include "network.h"
  40.  
  41. #ifdef NOMORPHOS
  42. static struct EmulLibEntry net_on_packet;
  43. #else
  44. static void SAVEDS net_on_packet(void *udata, ikspak *pak);
  45. #endif
  46. static void net_handle_msg(ikspak *pak);
  47. static void net_handle_iq(ikspak *pak);
  48.  
  49. struct netdata net;
  50. struct Library *SocketBase = NULL;
  51. static int SocketBaseRef = 0;
  52. static long signum = -1;
  53.  
  54.  
  55. int net_lala(void)
  56. {
  57.     if(SocketBase)
  58.     {
  59.         SocketBaseRef++;
  60.         return 1;
  61.     }
  62.  
  63.     if(signum == -1)
  64.     {
  65.         signum = AllocSignal(-1);
  66.         if(signum == -1) return(0);
  67.         net.sigmask = 1L << signum;
  68.     }
  69.  
  70.     SocketBase = OpenLibrary("bsdsocket.library", 0);
  71.     if(!SocketBase) return(0);
  72.     SocketBaseTags(SBTM_SETVAL(SBTC_SIGIOMASK), net.sigmask, TAG_DONE);
  73.     SocketBaseRef++;
  74.  
  75.     return 1;
  76. }
  77.  
  78.  
  79. void net_bibi(void)
  80. {
  81.     if(!SocketBase || SocketBaseRef == 0)
  82.     {
  83.         printf("DOH! report this to palpa\n");
  84.         return;
  85.     }
  86.  
  87.     SocketBaseRef--;
  88.     if(SocketBaseRef == 0)
  89.     {
  90.         CloseLibrary(SocketBase);
  91.         SocketBase = NULL;
  92.         FreeSignal(signum);
  93.         signum = -1;
  94.         net.sigmask = 0;
  95.     }
  96. }
  97.  
  98.  
  99. void net_connect(void)
  100. {
  101.     char *tmp;
  102.  
  103.     if(net.state != NET_OFF) return;
  104.  
  105.     if(!net_lala())
  106.     {
  107.         gui_state("Where is the TCP/IP stack?");
  108.         return;
  109.     }
  110.  
  111.     tmp = strdup(convert_utf8(prf.user));
  112.     net.id = iks_id_new(NULL, tmp);
  113.     free(tmp);
  114.     if(!net.id || !net.id->server || !net.id->user)
  115.     {
  116.         gui_state("Check your Jabber ID");
  117.         net_bibi();
  118.         return;
  119.     }
  120.     if(!net.id->resource)
  121.         /* FIX ME */ net.id->resource = "Jabberwocky";
  122.  
  123.     net.parser = iks_jabber_new(&net, &net_on_packet);
  124.     if(!net.parser) return;
  125.     iks_set_logging(net.parser, &console_logger);
  126.  
  127.     net.state = NET_CONNECT;
  128.     gui_state("Connecting...");
  129.     if(!iks_connect_tcp(net.parser, net.id->server, 0))
  130.     {
  131.         net_disconnect("Cannot connect!");
  132.         return;
  133.     }
  134. }
  135.  
  136.  
  137. void net_disconnect(const char *msg)
  138. {
  139.     net.state = NET_OFF;
  140.     net.sigmask = 0;
  141.  
  142.     if(net.parser)
  143.     {
  144.         iks_parser_delete(net.parser);
  145.         net.parser = NULL;
  146.     }
  147.  
  148.     roster_reset();
  149.  
  150.     net_bibi();
  151.     gui_state(msg);
  152. }
  153.  
  154.  
  155. char *net_myip(void)
  156. {
  157.     return (char *) iks_myip(net.parser);
  158. }
  159.  
  160.  
  161. void net_listen(void)
  162. {
  163.     http_listen();
  164.     if(net.state == NET_OFF) return;
  165.     iks_recv(net.parser, 0);
  166. }
  167.  
  168.  
  169. #ifdef NOMORPHOS
  170. static void net_on_packet_gate(void);
  171.  
  172. static struct EmulLibEntry net_on_packet =
  173. {
  174.     TRAP_LIB, 0, (void (*)(void)) net_on_packet_gate
  175. };
  176.  
  177. static void net_on_packet_gate(void)
  178. {
  179.     ULONG *lala = (ULONG *)REG_A7;
  180.     void *udata = *lala++;
  181.     ikspak *pak = *lala++;
  182. #else
  183. static void SAVEDS net_on_packet(void *udata, ikspak *pak)
  184. {
  185. #endif
  186.     iks *x;
  187.     if(!pak)
  188.     {
  189.         gui_state("Connection reset!");
  190.         return;
  191.     }
  192.     
  193.     switch(pak->type)
  194.     {
  195.     case IKS_PAK_STREAM:
  196.         net.streamid = iks_find_attrib(pak->x, "id");
  197.         if(prf.auth_method) net.streamid = NULL;
  198.         if(net.regflag)
  199.         {
  200.             iks *y;
  201.             x = iks_make_iq(IKS_TYPE_SET, IKS_NS_REGISTER);
  202.             iks_insert_attrib(x, "id", "reg");
  203.             y = iks_child(x);
  204.             iks_insert_cdata(iks_insert(y, "username"), net.id->user, -1);
  205.             iks_insert_cdata(iks_insert(y, "password"), prf.pass, -1);
  206.             iks_send(net.parser, x);
  207.             iks_delete(x);
  208.             net.state = NET_REG;
  209.             gui_state("Registering...");
  210.             break;
  211.         }
  212.         x = iks_make_auth(net.id, prf.pass, net.streamid);
  213.         iks_insert_attrib(x, "id", "auth");
  214.         iks_send(net.parser, x);
  215.         iks_delete(x);
  216.         net.state = NET_AUTH;
  217.         gui_state("Logging in...");
  218.         break;
  219.  
  220.     case IKS_PAK_MESSAGE:
  221.         net_handle_msg(pak);
  222.         break;
  223.  
  224.     case IKS_PAK_PRESENCE:
  225.         if(gchat_packet(pak)) {
  226.             con_debug("got unsupported groupchat packet");
  227.             break;
  228.         }
  229.         roster_update_presence(pak);
  230.         break;
  231.  
  232.     case IKS_PAK_S10N:
  233.         presence_ask(pak);
  234.         break;
  235.  
  236.     case IKS_PAK_IQ:
  237.         net_handle_iq(pak);
  238.         break;
  239.  
  240.     case IKS_PAK_ERROR:
  241.         gui_state("Stream Error");
  242.         break;
  243.  
  244.     default:
  245.         con_debug("Unknown packet.");
  246.  
  247.  
  248.         iks_delete(pak->x);
  249.     }
  250. }
  251.  
  252.  
  253. static void net_handle_msg(ikspak *pak)
  254. {
  255.     juser u;
  256.     jmessage *m;
  257.  
  258.     if(pak->subtype == IKS_TYPE_GROUPCHAT)
  259.     {
  260.         prf_event(EVT_GROUPCHAT);
  261.         gchat_packet(pak);
  262.         return;
  263.     }
  264.  
  265.     if ((pak->subtype == IKS_TYPE_CHAT && !prf.chatAsMessage) ||
  266.             pak->subtype == IKS_TYPE_NONE && prf.messageAsChat) {
  267.         prf_event(EVT_CHAT);
  268.         chat_packet(pak);
  269.         return;
  270.     }
  271.  
  272.     u = roster_locate(pak->from);
  273.     if(!u)
  274.     {
  275.         con_debug("cannot place user on roster!");
  276.         return;
  277.     }
  278.  
  279.     m = malloc(sizeof(jmessage));
  280.     if(!m) return; /* FIXME */
  281.     m->pak = pak;
  282.     m->flags = 0;
  283.     list_append(&u->msgs, m);
  284.  
  285.     prf_event(EVT_MESSAGE);
  286.  
  287.     u->event++;
  288.     roster_refresh(u);
  289.     if(u->readwin) {
  290.         DoMethod(u->readwin, READ_UPDATE);
  291.     }
  292. }
  293.  
  294.  
  295. static void net_handle_iq(ikspak *pak)
  296. {
  297.  
  298.     if(iks_strcmp(iks_find_attrib(pak->x, "id"), "auth") == 0)
  299.     {
  300.         if(pak->subtype == IKS_TYPE_RESULT)
  301.         {
  302.             iks *x;
  303.             net.state = NET_FETCH;
  304.             gui_state("Fetching roster...");
  305.             agents_fetch();
  306.             roster_fetch();
  307.         }
  308.         else
  309.         {
  310.             gui_state("Authorization failed!");
  311.         }
  312.         return;
  313.     }
  314.  
  315.     else if(iks_strcmp(iks_find_attrib(pak->x, "id"), "reg") == 0)
  316.     {
  317.         if(pak->subtype == IKS_TYPE_RESULT)
  318.         {
  319.             iks *x;
  320.             net.state = NET_AUTH;
  321.             gui_state("Logging in...");
  322.             x = iks_make_auth(net.id, prf.pass, net.streamid);
  323.             iks_insert_attrib(x, "id", "auth");
  324.             iks_send(net.parser, x);
  325.             iks_delete(x);
  326.         }
  327.         else
  328.         {
  329.             gui_state("Registration failed!");
  330.         }
  331.         return;
  332.     }
  333.     
  334.     if(iks_strcmp(pak->ns, IKS_NS_VERSION) == 0)
  335.     {
  336.         switch(pak->subtype)
  337.         {
  338.         case IKS_TYPE_GET:
  339.         {
  340.             iks *x, *y;
  341.             x = iks_make_iq(IKS_TYPE_RESULT, IKS_NS_VERSION);
  342.             iks_insert_attrib(x, "to", iks_id_print(pak->from));
  343.             y = iks_find(x, "query");
  344.             iks_insert_cdata(iks_insert(y, "name"), "Jabberwocky", 12);
  345.             iks_insert_cdata(iks_insert(y, "version"), about_version(), -1);
  346.             iks_insert_cdata(iks_insert(y, "os"), "AmigaOS", 7);
  347.             iks_send(net.parser, x);
  348.             iks_delete(x);
  349.             return;
  350.         }
  351.         case IKS_TYPE_RESULT:
  352.             userinfo_update(pak);
  353.             return;
  354.         }
  355.     }
  356.  
  357.     if(iks_strcmp(pak->ns, IKS_NS_ROSTER) == 0)
  358.     {
  359.         roster_update(pak);
  360.         return;
  361.     }
  362.  
  363.     if(iks_strcmp(pak->ns, IKS_NS_AGENTS) == 0)
  364.     {
  365.         DoMethod(gui.agentswin, AGENTS_PARSE, pak);
  366.         return;
  367.     }
  368.  
  369.     if(iks_strcmp(pak->ns, IKS_NS_REGISTER) == 0)
  370.     {
  371.         register_this(pak);
  372.         return;
  373.     }
  374.  
  375.     if(iks_strcmp(pak->ns, IKS_NS_SEARCH) == 0)
  376.     {
  377.         search_display(pak);
  378.         return;
  379.     }
  380.  
  381.     if(iks_strcmp(pak->ns, IKS_NS_OOB) == 0)
  382.     {
  383.         xfer_packet(pak);
  384.         return;
  385.     }
  386.     
  387.     {
  388.         char errorMsg[100];
  389.         #define UNKNOWN_IQ_PACKET_MSG "unknown iq packet: "
  390.         
  391.         strcpy(errorMsg, UNKNOWN_IQ_PACKET_MSG);
  392.         strncpy(errorMsg + strlen(UNKNOWN_IQ_PACKET_MSG), pak->ns ? pak->ns : "null", 99 - strlen(UNKNOWN_IQ_PACKET_MSG));
  393.         errorMsg[100] = 0;
  394.         con_debug(errorMsg);
  395.     }
  396. }
  397.  
  398.  
  399. /*
  400.         x = xmlnode_get_tag(p->x, "error");
  401.         net_disconnect(xmlnode_get_data(xmlnode_get_firstchild(x)));
  402. */
  403.